﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace Navigant.Web.UI
{
    public class EnhancedSPGridView : SPGridView
    {
        #region Grouping

        public event EventHandler RequiresDataSource;

        protected override void LoadControlState(object savedState)
        {
            base.LoadControlState(savedState);
            if (this.DataSource == null)
            {
                this.InvokeRequiresDataSource();
            }
        }

        protected void InvokeRequiresDataSource()
        {
            EventHandler handler = this.RequiresDataSource;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        #endregion

        #region Paging

        #region Private Constants

        private const string ItemCountViewStateKey = "sppgv_virtitemcount";
        private const string PageIndexViewStateKey = "sppgv_currentpageindex";
        private const int ItemCountNoItems = -1;

        #endregion Private Constants

        #region Overrides

        protected override void InitializePager(GridViewRow row, int columnSpan, 
            PagedDataSource pagedDataSource)
        {
            //We override InitializePager to set a few properties before InitializePager is called
            //from base class. This way we will implement custom paging but will still use the
            //default pager control.
            if (this.CustomPagingTurnedOn)
            {
                pagedDataSource.AllowCustomPaging = true;
                pagedDataSource.VirtualCount = this.VirtualItemCount;
                pagedDataSource.CurrentPageIndex = this.CurrentPageIndex;
            }
            base.InitializePager(row, columnSpan, pagedDataSource);
        }

        #endregion Overrides

        #region Public Properties

        /// <summary>
        /// Gets or sets the total number of items in result set no matter
        /// how many items will be shown per page.
        /// For example, if total number of items that should be shown by 
        /// the grid is 2356002 but only 10 items should be shown per page then
        /// this property will be set to 2356002.
        /// </summary>
        public int VirtualItemCount
        {
            get
            {
                int itemCount = EnhancedSPGridView.ItemCountNoItems;

                if (ViewState[EnhancedSPGridView.ItemCountViewStateKey] == null)
                {
                    ViewState[EnhancedSPGridView.ItemCountViewStateKey] = EnhancedSPGridView.ItemCountNoItems;
                }
                else
                {
                    itemCount = Convert.ToInt32(ViewState[EnhancedSPGridView.ItemCountViewStateKey], CultureInfo.InvariantCulture);
                }
                return itemCount;
            }
            set
            {
                ViewState[EnhancedSPGridView.ItemCountViewStateKey] = value;
            }
        }

        /// <summary>
        /// Gets or sets the object from which the data-bound control retrieves its list of
        /// data items. This property should be set to a list of items that should be shown
        /// on a particular/current page.
        /// </summary>
        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                base.DataSource = value;
                // we store the page index here so we dont lose it during DataBind
                this.CurrentPageIndex = this.PageIndex;
            }
        }

        #endregion Public Properties

        #region Private Properties

        private bool CustomPagingTurnedOn
        {
            get
            {
                //If VirtualItemCount is set (any value different than -1) then
                //we say custom paging is turned on.
                return (this.VirtualItemCount != EnhancedSPGridView.ItemCountNoItems);
            }
        }

        private int CurrentPageIndex
        {
            get
            {
                int currentPageIndex = 0;
                if (this.ViewState[EnhancedSPGridView.PageIndexViewStateKey] == null)
                {
                    this.ViewState[EnhancedSPGridView.PageIndexViewStateKey] = 0;
                }
                else
                {
                    currentPageIndex = Convert.ToInt32(this.ViewState[EnhancedSPGridView.PageIndexViewStateKey], 
                        CultureInfo.InvariantCulture);
                }
                return currentPageIndex;
            }
            set
            {
                this.ViewState[EnhancedSPGridView.PageIndexViewStateKey] = value;
            }
        }

        #endregion Private properties

        #endregion
    }
}
